﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace MyPublisher.PublishService
{
    static class Program
    {
        public static String ServiceName = "IKWOnlinePublishService";
        public static String ServiceNameDisplay = "IKWOnline Publish Service(IKWOnlinePublishService)";
        public static readonly String ServiceNameDescription = "IKWOnline Publish Service(IKWOnlinePublishService)";
        public static bool quit = false;

        static void Main()
        {
            try
            {
                string[] argv = Environment.GetCommandLineArgs();
                bool RunNoService = false;
                for (int i = 0, count = argv.Length; i <count; ++i)
                {
                    string arg = argv[i].ToLower();
                    if ( (arg.CompareTo("/service") == 0) || (arg.CompareTo("/s") == 0) || (arg.CompareTo("/i") == 0))
                    {
                        if (i < count - 1 && !arg.StartsWith("\\"))
                        {
                            ServiceName = argv[++i];
                        }
                        RegisterService();
                        return;
                    }
                    else if ( (arg.CompareTo("/unregserver") == 0) || (arg.CompareTo("/u") == 0))
                    {
                        if (i < count - 1 && !arg.StartsWith("\\"))
                        {
                            ServiceName = argv[++i];
                        }
                        RemoveService();
                        return;
                    }
                    else if ( (arg.CompareTo("/notservice") == 0) || (arg.CompareTo("/ns") == 0) || (arg.CompareTo("/ni") == 0) )
                    {
                        Console.WriteLine("Not running as a service");
                        RunNoService = true;
                        break;
                    }
                }

                if (RunNoService)
                {
                    DebugRun();
                }
                else
                {
                    ServiceRun();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        private static void DebugRun()
        {
            Service service = new Service();
            try
            {
                service.DebugOnStart();
                Console.WriteLine("Service started.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Service error.",e.Message);
                Console.ReadLine();
                service.DebugOnStop();
                return;
            }
            Console.ReadLine();
            service.DebugOnStop();
        }

        private static void ServiceRun()
        {
            ServiceBase[] ServicesToRun = new ServiceBase[] { new Service() };
            ServiceBase.Run(ServicesToRun);
        }

        private static void RegisterService()
        {
            try
            {
                Console.WriteLine("Service:{0}", ServiceName);

                string sProgramWithFullPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                sProgramWithFullPath = sProgramWithFullPath.Replace(".vshost", "");
                if (!ServiceManager.RegisterService(ServiceName, ServiceNameDisplay, ServiceNameDescription, sProgramWithFullPath))
                {
                    throw new ApplicationException("Failed to register service.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private static void RemoveService()
        {
            try
            {
                Console.WriteLine("UnRegServer:{0}", ServiceName);

                if (!ServiceManager.RemoveService(ServiceName))
                {
                    throw new ApplicationException("Failed to unregister service");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
